<?php

if (isset($_REQUEST["poz"])){
	if ($_REQUEST["poz"] != "neperk" && $_REQUEST["poz"] != "new" && $_REQUEST["poz"] != "unconf" && $_REQUEST["poz"] != "conf" && $_REQUEST["poz"] != "all") {
		$poz = "new";
	} else {
		$poz = $_REQUEST["poz"];
	}
} else {
	$poz = "new";
}
if ($poz == "all") {
	$filter = "";
	$filter2 = "";
	$sort = " ORDER BY I31_OP_DATA DESC";
} else if ($poz == "new") {
// 2012-11-05 ridas
	//$filter = "and ((I31_KODAS_LS_4 <> '+' and I31_KODAS_LS_4 <> '-') or I31_KODAS_LS_4 is null)";
	//$filter2 = "and ((i06_kodas_ls_4 <> '+' and i06_kodas_ls_4 <> '-') or i06_kodas_ls_4 is null)";
	$filter = "and ((I31_KODAS_LS_4 <> '+') or I31_KODAS_LS_4 is null)";
	$filter2 = "and ((i06_kodas_ls_4 <> '+') or i06_kodas_ls_4 is null)";
	$sort = " ORDER BY I31_OP_DATA DESC";
} else if ($poz == "conf") {
	$filter = "and I31_KODAS_LS_4 = '+'";
	$filter2 = "and i06_kodas_ls_4 = '+'";
	$sort = " ORDER BY I31_OP_DATA DESC";
} else if ($poz == "unconf") {
	$filter = "and I31_KODAS_LS_4 = '-'";
	$filter2 = "and i06_kodas_ls_4 = '-'";
	$sort = " ORDER BY I31_OP_DATA DESC";
} else if ($poz == "neperk") {
	//$filter = "AND I31_PERKELTA=2";  //for testing
	$filter = "AND I31_PERKELTA=1";
	$filter2 = "and I06_PERKELTA=1";	
	$sort = " ORDER BY I31_DOK_NR";
} else {
	$filter = "";
	$filter2 = "";
}


	if (!isset($adm_menu)) {
		if (!empty($minfo)) {
			$accounts_query = str_replace("<im_kodas>", trim($minfo["N08_kodas_ks"]), $accounts_query);
			$accounts_query = str_replace("<suma>", "sum(I32_DEB_SUMA) as I32_DEB_SUMA ", $accounts_query);
			
			$accounts_query .= $filter;
			if ($poz != "neperk") {
				$accounts_query .= " AND I31_PERKELTA=2 ";				
			}
			$accounts_query .= " group by i31_kodas_kr,I31_DOK_NR,I31_PASTABOS,I31_PERKELTA,I31_KODAS_LS_4,I31_OP_DATA,I32_DOK_NR,I32_KODAS_KS ";
			//echo $accounts_query;
			$accounts = $db_www->returnList($accounts_query . $sort);	
			
			$fakt = $db_www->returnInfo($fakt_query);
			$accounts_query2 = str_replace("<im_kodas>", trim($minfo["N08_kodas_ks"]), $accounts_query2);
			if (!empty($fakt)){
				$arr = explode(",",trim($fakt["N23_REIKSME1"]));
				$filter2 .= " and (1<>1 ";
				foreach ($arr as $item) {
					$filter2 .= " OR I06_DOK_NR like '".$item."%'";
				}
				$filter2 .= ")";
				$accounts_query2 .= $filter2;
			}
			if ($poz != "neperk") {
			// 2012-11-05 ridas
				$accounts_query2 .= " and (I06_PERKELTA=2 or I06_PERKELTA=3)";				
			}
			$accounts2 = $db_www->returnList($accounts_query2 . " ORDER BY I06_OP_DATA DESC");	
			
		} else {
			die("Nerasta vartotojo įmonė.");
		}
	} else { //adminas
			if (isset($_REQUEST['from'])){
				$_SESSION['from'] = $_REQUEST['from'];
			} else {
				if (!isset($_SESSION['from'])) {
					$_SESSION['from'] = subtractDaysFromToday(60);
				} 
			}
			
			if (isset($_REQUEST['to'])){
				$_SESSION['to'] = $_REQUEST['to'];
			} else {
				if (!isset($_SESSION['to'])) {
					$_SESSION['to'] = date("Y.m.d");
				} 
			}				
			
	
			if ($kreditas != "0" ) {
				$accounts_query = str_replace("<im_kodas>", "xxx' OR I32_KODAS_KS IN (SELECT N08_kodas_ks
	FROM N08_KLIJ INNER JOIN n60_asm ON N08_KLIJ.N08_KODAS_KS = n60_asm.n60_kodas_ks
	and n60_asm.n60_kodas_sm in (SELECT n60_kodas_sm FROM n60_asm where n60_poz_date<>1 and n60_kodas_gs='WEB')) OR I32_KODAS_KS ='xxx", $accounts_query);
				$accounts_query = str_replace("<suma>", "sum(I32_DEB_SUMA) as I32_DEB_SUMA ", $accounts_query);
				
				$accounts_query .= $filter;
				$accounts_query .= " AND I31_OP_DATA BETWEEN CONVERT(datetime,'".str_replace(".","-",$_SESSION['from'])."',102) AND CONVERT(datetime,'".str_replace(".","-",$_SESSION['to'])."',102)";
				if ($poz != "neperk") {					
					$accounts_query .= " AND I31_PERKELTA=2 ";
				} 
				$accounts_query .= " group by i31_kodas_kr,I31_DOK_NR,I31_PASTABOS,I31_PERKELTA,I31_KODAS_LS_4,I31_OP_DATA,I32_DOK_NR,I32_KODAS_KS ";
				//echo $accounts_query;
				$accounts = $db_www->returnList($accounts_query . " ORDER BY I31_OP_DATA DESC,I32_DOK_NR");	
			}
			$fakt = $db_www->returnInfo($fakt_query);
			$accounts_query2 = str_replace("<im_kodas>", "xxx' OR I06_KODAS_KS <>'xxx", $accounts_query2);
			if (!empty($fakt)){
				$arr = explode(",",trim($fakt["N23_REIKSME1"]));
				$filter2 .= " and (1<>1 ";
				foreach ($arr as $item) {
					$filter2 .= " OR I06_DOK_NR like '".$item."%'";
				}
				$filter2 .= ")";
				$accounts_query2 .= $filter2;
			}
			$accounts_query2 .= " AND I06_OP_DATA BETWEEN CONVERT(datetime,'".str_replace(".","-",$_SESSION['from'])."',102) AND CONVERT(datetime,'".str_replace(".","-",$_SESSION['to'])."',102)";
			//echo $accounts_query2;
			$accounts2 = $db_www->returnList($accounts_query2 . " ORDER BY I06_OP_DATA DESC");			
	}

?>